import java.util.HashMap;
import java.util.Map;

/**
 * 1419. 数青蛙
 */
class Solution1 {
    public int minNumberOfFrogs(String croakOfFrogs) {
        String s = "croak";
        char[] ch = croakOfFrogs.toCharArray();
        int len = s.length();
        int[] hash = new int[len];
        Map<Character, Integer> map = new HashMap<>();

        for(int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), i);
        }

        for(char x : ch) {
            int cur = map.get(x);
            if(cur == 0) {
                if(hash[len - 1] != 0) {
                    hash[len - 1]--;
                }
                hash[cur]++;
            } else {
                if(hash[cur - 1] > 0) {
                    hash[cur - 1]--;
                    hash[cur]++;
                } else {
                    return -1;
                }
            }
        }

        for(int i = 0; i < len - 1; i++) {
            if(hash[i] != 0) return -1;
        }

        return hash[len - 1];
    }
}